#include "my_gui.h"
#include "lvgl.h"

static lv_obj_t *obj;
static lv_obj_t *obj1;

void gui_base(void)
{
    #if 1
    lv_obj_t *obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj, 400, 300);
    lv_obj_set_x(obj, 100);
    lv_obj_set_y(obj, 50);
    lv_obj_set_pos(obj, 20, 20);

    lv_obj_set_style_bg_color(obj, lv_color_hex(0xf4b183), LV_STATE_DEFAULT);

    //lv_obj_t *obj1 = lv_obj_create(obj);
    //lv_obj_set_align(obj1, LV_ALIGN_CENTER);
    //lv_obj_align(obj1, LV_ALIGN_BOTTOM_MID, 50, 50);


    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_bg_color(&style, lv_color_hex(0xf4b183));

    lv_obj_t *obj2 = lv_obj_create(lv_scr_act());
    lv_obj_align_to(obj2, obj, LV_ALIGN_BOTTOM_LEFT, 10, 10);
    lv_obj_add_style(obj2, &style, LV_STATE_DEFAULT);
    #if 0
    lv_obj_t *obj3 = lv_obj_create(lv_scr_act());
    lv_obj_set_align(obj3, LV_ALIGN_CENTER);
    lv_obj_set_style_bg_color(obj3, lv_color_hex(0xf4b183), LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(obj3, 10, LV_STATE_DEFAULT);
    lv_obj_set_style_border_opa(obj3, 20, LV_STATE_DEFAULT);

    lv_obj_t *slider = lv_slider_create(lv_scr_act());
    lv_obj_set_align(slider, LV_ALIGN_CENTER);
    lv_obj_set_style_bg_color(slider, lv_color_hex(0xf4b183), LV_STATE_DEFAULT|LV_PART_KNOB);
    #endif
    #endif
}

static void my_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    printf("%d \n", code);

    if (target == obj) {
        printf("obj......\n");
    } else if(target == obj1) {
        printf("obj1......\n");
    }
}

void gui_event(void)
{
    obj = lv_obj_create(lv_scr_act());
    lv_obj_add_event_cb(obj, my_event_cb, LV_EVENT_CLICKED, NULL);
    obj1 = lv_obj_create(lv_scr_act());
    lv_obj_add_event_cb(obj1, my_event_cb, LV_EVENT_LONG_PRESSED, NULL);
    lv_obj_align_to(obj1, obj, LV_ALIGN_OUT_RIGHT_MID, 50, 0);
}

void gui_label(void)
{
    lv_obj_t *label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "hello lvgl!");
    lv_label_set_text_static(label, "hello lvgl!");
    lv_label_set_text_fmt(label, "VALUE: %d", 99);
    lv_obj_set_style_bg_color(label, lv_color_hex(0xff0000), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(label, 100, LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_24, LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(label, lv_color_hex(0x00ff00), LV_STATE_DEFAULT);
    lv_label_set_recolor(label, true);
    lv_label_set_text(label, "hello #0000ff lvgl# AAAAAAAAAAAA");
    lv_obj_set_size(label, 100, 50);
    //lv_label_set_long_mode(label, LV_LABEL_LONG_DOT);
    lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL);
    //lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
    char *text = lv_label_get_text(label);
    printf("text: %s", text);
}

static void gui_btn_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    printf("%d \n", code);

    if (target == obj) {
        printf("obj......\n");
    } else if(target == obj1) {
        printf("obj1......\n");
    }
}

void gui_btn(void)
{
    lv_obj_t *btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, 200, 100);
    lv_obj_set_align(btn, LV_ALIGN_CENTER);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0x0000ff), LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn, gui_btn_cb, LV_EVENT_CLICKED, NULL);
}

static void gui_switch_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    bool state = lv_obj_has_state(target, LV_STATE_CHECKED);
    printf("state: %d\n", state);
}

void gui_switch(void)
{
    lv_obj_t *switch1 = lv_switch_create(lv_scr_act());
    lv_obj_set_size(switch1, 100, 50);
    lv_obj_set_align(switch1, LV_ALIGN_CENTER);
    lv_obj_set_style_bg_color(switch1, lv_color_hex(0xff0000), LV_PART_KNOB);
    //lv_obj_add_state(switch1, LV_STATE_CHECKED | LV_STATE_DISABLED);
    lv_obj_add_event_cb(switch1, gui_switch_cb, LV_EVENT_CLICKED, NULL);
}

static void gui_checkbox_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    bool state = lv_obj_has_state(target, LV_STATE_CHECKED);
    printf("state: %d\n", state);
}

void gui_checkbox(void)
{
    lv_obj_t *checkbox = lv_checkbox_create(lv_scr_act());
    lv_obj_set_align(checkbox, LV_ALIGN_CENTER);
    lv_checkbox_set_text(checkbox, "remenber");
    lv_obj_set_style_pad_column(checkbox, 50, LV_STATE_DEFAULT); //设置间距
    //lv_obj_add_state(checkbox, LV_EVENT_CLICKED);
    //lv_obj_clear_state(checkbox, LV_EVENT_CLICKED);
    lv_obj_add_event_cb(checkbox, gui_checkbox_cb, LV_EVENT_CLICKED, NULL);
}

static void gui_bar_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    int32_t state = lv_bar_get_value(target);
    printf("state: %d\n", state);
}

void gui_bar(void)
{
    lv_obj_t *bar = lv_bar_create(lv_scr_act());
    lv_obj_set_align(bar, LV_ALIGN_CENTER);
    //lv_obj_set_size(bar, 20, 400);
    lv_bar_set_range(bar, 0, 100);
    //lv_obj_set_style_anim_time(bar, 500, LV_STATE_DEFAULT); //必须放在设置值之前才可以看到动画效果
    lv_bar_set_value(bar, 80, LV_ANIM_ON);
    lv_bar_set_mode(bar, LV_BAR_MODE_RANGE);    //扩展：模式设置
    //lv_bar_set_start_value(bar, 20, LV_ANIM_OFF);
}

void gui_rect_bar(void)
{
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 0);
    lv_style_set_bg_color(&style, lv_color_hex(0xffffff));
    lv_style_set_border_width(&style, 2);
    lv_style_set_border_color(&style, lv_color_hex(0x000000));

    lv_style_set_pad_top(&style, 5);
    lv_style_set_pad_bottom(&style, 5);
    lv_style_set_pad_left(&style, 5);
    lv_style_set_pad_right(&style, 4);

    lv_obj_t* bar = lv_bar_create(lv_scr_act());
    lv_bar_set_range(bar, 0, 100);
    lv_obj_add_style(bar, &style, LV_STATE_DEFAULT);
    lv_obj_set_size(bar, 50, 300);
    lv_obj_align(bar, LV_ALIGN_CENTER, 0, 0);

    lv_obj_set_style_radius(bar, 0, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(bar, lv_color_hex(0x00ff00), LV_PART_INDICATOR);
    lv_bar_set_value(bar, 80, LV_ANIM_ON);
}

void gui_spinner(void)
{
    lv_obj_t *spinner = lv_spinner_create(lv_scr_act(), 1000, 50);
    lv_obj_set_style_arc_color(spinner, lv_color_hex(0xff0000), LV_PART_MAIN);
    lv_obj_set_style_arc_color(spinner, lv_color_hex(0x0000ff), LV_PART_INDICATOR);
    lv_obj_set_style_arc_width(spinner, 30, LV_PART_MAIN);
    lv_obj_set_style_arc_width(spinner, 30, LV_PART_INDICATOR);
}

void gui_led(void)
{
    lv_obj_t *led = lv_led_create(lv_scr_act());
    //lv_obj_set_align(led, LV_ALIGN_CENTER);
    lv_obj_center(led);
    lv_led_set_color(led, lv_color_hex(0x00ff00));
    //lv_led_set_brightness(led, 255);
    lv_led_off(led);
    lv_led_on(led);
    lv_led_toggle(led); //翻转
}

static lv_obj_t *list;
static void gui_list_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("list btn: %s\n", lv_list_get_btn_text(list, target));
}

void gui_list(void)
{
    list = lv_list_create(lv_scr_act());
    lv_obj_center(list);
    lv_list_add_text(list, "Settings");
    lv_obj_t *btn0 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "AAA");
    lv_obj_t *btn1 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "BBB");
    lv_obj_t *btn2 = lv_list_add_btn(list, LV_SYMBOL_WIFI, "CCC");
    lv_obj_add_event_cb(btn0, gui_list_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(btn1, gui_list_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(btn2, gui_list_cb, LV_EVENT_CLICKED, NULL);
}

static void gui_dropdown_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("dropdown: %d\n", lv_dropdown_get_selected(target));
    char buf[10];
    lv_dropdown_get_selected_str(target, buf, sizeof(buf));
    printf("dropdown: %s\n", buf);
}

void gui_dropdown(void)
{
    lv_obj_t *dd = lv_dropdown_create(lv_scr_act());
    lv_dropdown_set_options(dd, "a\nb\nc\nd");
    //lv_dropdown_set_options_static(dd, "a\nb\nc\nd");
    lv_dropdown_add_option(dd, "e", 4);
    lv_dropdown_set_selected(dd, 3);
    lv_dropdown_set_dir(dd, LV_DIR_RIGHT);
    lv_dropdown_set_symbol(dd, LV_SYMBOL_AUDIO);
    lv_obj_add_event_cb(dd, gui_dropdown_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void gui_roller_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("roller: %d\n", lv_roller_get_selected(target));
    char buf[10];
    lv_roller_get_selected_str(target, buf, sizeof(buf));
    printf("roller: %s\n", buf);
}

void gui_roller(void)
{
    lv_obj_t *roller = lv_roller_create(lv_scr_act());
    lv_obj_center(roller);
    lv_obj_set_style_text_line_space(roller, 30, LV_STATE_DEFAULT);
    lv_roller_set_options(roller, "a\nb\nc\nd\ne", LV_ROLLER_MODE_NORMAL); //循环
    lv_roller_set_selected(roller, 3, LV_ANIM_ON);
    lv_roller_set_visible_row_count(roller, 2);
    lv_obj_add_event_cb(roller, gui_roller_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void gui_slider_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("slider: %d\n", lv_slider_get_value(target));
}

void gui_slider(void)
{
    lv_obj_t *slider = lv_slider_create(lv_scr_act());
    lv_obj_center(slider);
    lv_obj_set_size(slider, 300, 20);
    lv_slider_set_value(slider, 50, LV_ANIM_OFF);
    lv_slider_set_range(slider, -100, 100);
    lv_obj_add_event_cb(slider, gui_slider_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static void gui_arc_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("arc: %d\n", lv_arc_get_value(target));
}

void gui_arc(void)
{
    lv_obj_t *arc = lv_arc_create(lv_scr_act());
    lv_obj_center(arc);
    lv_arc_set_range(arc, 0, 200);
    lv_arc_set_value(arc, 120);
    lv_arc_set_angles(arc, 135, 270);
    lv_arc_set_bg_angles(arc, 135, 45);
    lv_obj_add_event_cb(arc, gui_arc_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

void gui_line(void)
{
    lv_obj_t *line = lv_line_create(lv_scr_act());
    lv_obj_center(line);
    static lv_point_t line_points[] = {{150,50},{250,200},{50,200},{150,50}};
    lv_line_set_points(line, line_points, 4);
    lv_obj_set_style_line_width(line, 8, LV_PART_MAIN);
    lv_obj_set_style_line_rounded(line, true, LV_PART_MAIN);
    lv_line_set_y_invert(line, true);
}

static void gui_img_cb(lv_event_t *e)
{
    printf("AAAAAAAAAAAAAAAAAA\n");
}

LV_IMG_DECLARE(img_lv_100ask_icon_floder);
void gui_img(void)
{
    lv_obj_t *img = lv_img_create(lv_scr_act());
    lv_obj_center(img);
    lv_img_set_src(img, &img_lv_100ask_icon_floder);
    lv_obj_add_event_cb(img, gui_img_cb, LV_EVENT_CLICKED, NULL);
}

void gui_colorwheel(void)
{
    lv_obj_t *cw = lv_colorwheel_create(lv_scr_act(), true);
    lv_obj_center(cw);
}

static void gui_btnmatrix_cb(lv_event_t *e)
{
    uint8_t id;
    lv_obj_t *target = lv_event_get_target(e);
    id = lv_btnmatrix_get_selected_btn(target);
    printf("btnmatrix: %d\n", id);
    printf("btnmatrix: %s\n", lv_btnmatrix_get_btn_text(target, id));
}
void gui_btnmatrix(void)
{
    lv_obj_t *btnm = lv_btnmatrix_create(lv_scr_act());
    lv_obj_center(btnm);
    static const char *map[] = {"1","#ff0000 2#","3","\n","4","5","6","\n","7","8","9","\n","0","=",""};
    lv_btnmatrix_set_map(btnm, map);
    lv_btnmatrix_set_btn_width(btnm, 10, 2);
    lv_btnmatrix_set_one_checked(btnm, true); //单次选中
    lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED); //去掉使能
    lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_RECOLOR); //重新着色
    lv_btnmatrix_set_btn_ctrl_all(btnm, LV_BTNMATRIX_CTRL_CHECKABLE); //选中着色
    lv_obj_add_event_cb(btnm, gui_btnmatrix_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

LV_FONT_DECLARE(ganggang)
void gui_app_font(void)
{
    lv_obj_t *font_label = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_font(font_label, &ganggang, LV_STATE_DEFAULT);
    lv_label_set_text(font_label, "罡罡冰壶");
    lv_obj_center(font_label);
}

void my_gui(void)
{
    //gui_base();
    //gui_event();
    //gui_label();
    //gui_btn();
    //gui_switch();
    //gui_checkbox();
    //gui_bar();
    //gui_rect_bar();
    //gui_spinner();
    //gui_led();
    //gui_list();
    //gui_dropdown();
    //gui_roller();
    //gui_slider();
    //gui_arc();
    //gui_line();
    //gui_img();
    //gui_colorwheel();
    //gui_btnmatrix();
    gui_app_font();
}
